layui.use([], function () {
    var form = layui.form
        , layer = layui.layer
        , $ = layui.$;
    $("#projectId").val(GetQueryString("id"));
    $("#num").val(GetQueryString("num"));
    $("#price").val(GetQueryString("price"));
    $("#allMoney").val(parseFloat(GetQueryString("num")) * parseFloat(GetQueryString("price")));


    //提交支付
    form.on('submit(support-submit)', function (data) {
            layer.alert(JSON.stringify(data.field), {
                title: '最终的提交信息'
            });
            $.ajax({
                url: "/home/support",
                data: data.field,
                success: function (d) {
                    alert(d.msg);
                }
            });
            return false;
        }
    );


    //添加地址
    $("#addAddress").click(function () {
        $.get('address.html', {}, function (str) {
            layer.open({
                type: 1,
                title: "添加地址",
                area: ['1000px', '600px'],
                content: str,//注意，如果str是object，那么需要字符拼接。
                success: function (layero, index) {
                    // form.render();
                    //监听添加地址提交
                    var c = 0;
                    form.on('submit(address-submit)', function (data) {
                        c++;
                        if (c == 1) {
                            data.field.addressProvide = $("#provid").children("option[selected]").text();
                            data.field.addressCity = $("#cityid").children("option[selected]").text();
                            data.field.addressArea = $("#areaid").children("option[selected]").text();
                            layer.alert(JSON.stringify(data.field), {
                                title: '最终的提交信息'
                            });
                            $.ajax({
                                url: "/home/addUserAddress",
                                data: data.field,
                                async: false,
                                success: function (d) {
                                    layer.closeAll();
                                    layer.msg(d.msg);
                                    var addr = "[收货人：" + data.field.consigneeName + "]" + "[  联系电话：" + data.field.addressPhone + "]" + "地址：" + data.field.addressProvide + data.field.addressCity + data.field.addressArea + data.field.addressDetail;
                                    var html = "      <div class=\"layui-form-item\">\n" +
                                        "            <div class=\"layui-input-block\" style=\"margin-left: 0px\">\n" +
                                        "                <input type=\"radio\" name=\"address\" value=\"" + addr + "\" title=\"" + addr + "\" checked=\"\">\n" +
                                        "            </div>\n" +
                                        "        </div>"

                                    $(".layui-form").append(html);
                                    form.render();

                                }
                            });
                        }
                        return false;
                    });
                }
            });
        });
    });


    var addressList;

    //获取地址
    $.ajax({
        url: "/home/getUserAllAddress",
        success: function (data) {
            // console.log(data.data);
            addressList = data.data;
            var html = "";
            $.each(data.data, function (idx, obj) {

                var addr = "[收货人：" + obj.consigneeName + "]" + "[  联系电话：" + obj.addressPhone + "]" + "地址：" + obj.addressProvid + obj.addressCity + obj.addressArea + obj.addressDetail;
                var check = ""
                if (obj.isDefault == 1) {
                    check = "checked=''";
                    html = "      <div  class=\"layui-form-item\">\n" +
                        "            <div class=\"layui-input-block\" style=\"margin-left: 0px\">\n" +
                        "                <input id='addressRadio' type=\"radio\" name=\"address\" value=\"" + addr + "\" title=\"" + addr + "\" " + check + ">\n" +
                        "            </div>\n" +
                        "        </div>"
                }

            });
            $(".layui-form").prepend(html);
            form.render();
        }
    });

    function cAddress(id) {
        // $("#address").remove();
        $.each(addressList, function (k, obj) {
            if (obj.addressId == id) {
                console.log(obj)
                var addr = "[收货人：" + obj.consigneeName + "]" + "[  联系电话：" + obj.addressPhone + "]" + "地址：" + obj.addressProvid + obj.addressCity + obj.addressArea + obj.addressDetail;
                $("#addressRadio").val(addr);
                $("#addressRadio").attr("title",addr);
                $("#addressRadio").attr("checked",'');
                form.render("radio");
            }
        });

    }


    //切换地址
    $("#changeAddress").click(function () {

        //获取地址
        $.ajax({
            url: "/home/getUserAllAddress",
            success: function (data) {
                console.log(data.data);
                if (data.data.length < 1) {
                    alert("没有地址！");
                    return;
                }
                var html = "<form class='layui-form' id='c-address-form'>";
                $.each(data.data, function (idx, obj) {
                    var addr = "[收货人：" + obj.consigneeName + "]" + "[  联系电话：" + obj.addressPhone + "]" + "地址：" + obj.addressProvid + obj.addressCity + obj.addressArea + obj.addressDetail;
                    var check = ""
                    if (obj.isDefault == 1) {
                        check = "checked=''";
                    }
                    html = html + "      <div class='layui-form-item'>\n" +
                        "            <div class='layui-input-block' >\n" +
                        "                <input type='radio' name='addressId' value='" + obj.addressId + "' title='" + addr + "' " + check + ">\n" +
                        "            </div>\n" +
                        "            </div>"


                });

                html += "            <div class='layui-form-item'>\n" +
                    "                    <div class='layui-input-block'>\n" +
                    "                    <button id='c-submit' class='layui-btn' lay-submit='' lay-filter=\"c-submit\">确定选择</button>\n" +
                    "                    </div>\n" +
                    "            </div>";
                html += "</form>";
                layer.open({
                    type: 1,
                    title: "切换地址",
                    area: ['1000px', '600px'],
                    content: html,//注意，如果str是object，那么需要字符拼接。
                    success: function (layero, index) {
                        form.render();
                        form.on('submit(c-submit)', function (data) {
                            cAddress(data.field.addressId);
                            layer.closeAll();
                            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
                        });
                    }
                });

            }

        });
    });


});
